package zelix.hack.hacks;

import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemSword;
import net.minecraft.network.play.client.CPacketPlayer;
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.event.InputUpdateEvent;
import net.minecraftforge.common.util.PacketUtil;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.input.Mouse;
import scala.collection.parallel.ParIterableLike;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.utils.MoveUtils;
import zelix.utils.Utils;
import zelix.utils.Wrapper;
import zelix.utils.system.Mapping;
import zelix.value.Mode;
import zelix.value.ModeValue;

import java.lang.reflect.Field;

import static zelix.hack.hacks.AimAssist.mc;

public class Regen extends Hack {


    Float ticks = 0.98f;
    Minecraft mc = Wrapper.INSTANCE.mc();
    ModeValue mode = new ModeValue("Mode" , new Mode("New" ,true) , new Mode("Old" , false));
    public Regen(){
        super("Regen" , HackCategory.PLAYER);
    }
    @Override
    public void onDisable() {
        setTickLength(50);
        super.onEnable();
    }

    @Override
    public void onInputUpdate(InputUpdateEvent event) {
        Utils.nullCheck();
        if (mode.getMode("New").isToggled()){
            if (mc.player.ticksExisted % 5 == 0) {
                setTickLength(50 / ticks.floatValue());


                for (int i = 0; i < 10; i++) {
                    Wrapper.INSTANCE.sendPacket(new CPacketPlayer(true));
                }


            } else {
                if (MoveUtils.isMoving()) Wrapper.INSTANCE.sendPacket(new CPacketPlayer(mc.player.onGround));
            }
        }else if (mode.getMode("Old").isToggled()){
            if (MoveUtils.isMoving() || !mc.player.onGround) {
                return;
            }
            for (int i = 0; i < 9; i++) {
                Wrapper.INSTANCE.sendPacket(new CPacketPlayer(mc.player.onGround));
            }

            setTickLength(50 / 0.45f);
        }
    }
    private void setTickLength(float tickLength)
    {
        try
        {
            Field fTimer = Minecraft.getMinecraft().getClass().getDeclaredField(Mapping.timer);
            fTimer.setAccessible(true);

            Field fTickLength = net.minecraft.util.Timer.class.getDeclaredField(Mapping.tickLength);
            fTickLength.setAccessible(true);
            fTickLength.setFloat(fTimer.get(Minecraft.getMinecraft()), tickLength);

        }catch(ReflectiveOperationException e)
        {
            throw new RuntimeException(e);
        }
    }
}
